# -*- coding: utf-8 -*-
# @Author  : Pony.guan
# @Time    : 2021/11/22 下午4:38
# Description: 创建10个工作站

import json
import requests

# 创建容器规格url  # todo 手动获取域名1
create_container_spec_url = 'http://nova-nginx-portal.xsys.svc.cluster.local/gw/wms/container/spec/' \
                            'createOrUpdateContainerSpec'
# 创建工作站url   # todo 手动获取域名2
create_station_url = 'http://nova-iwms-station.xsys.svc.cluster.local/station/createStationThroughESS'
# 创建播种墙url   # todo 手动获取域名1
create_put_wall_url = 'http://nova-nginx-portal.xsys.svc.cluster.local/gw/station/putWall/createOrUpdatePutWall'

# todo 手动获取Authorization
headers = {
    "Content-Type": "application/json",
    "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2Mzc3NTM3MDIsInVzZXJfbmFtZSI6ImFkbWluIiwiYXV0aG9yaXRpZXMiOlsiKjoqIiwiJCQkJiYmISEhUk9MRV9zdXBlcl9hZG1pbiJdLCJqdGkiOiIxMjQyMDVhMC0xNGIyLTQ2MjgtOGExYy1hNTdlMWUyZGJiMzUiLCJjbGllbnRfaWQiOiJhZG1pbl9jbGllbnQiLCJzY29wZSI6WyJhbGwiXX0.qvkcFJkEm87S-Zz2R342s9EqW16tanAq8M4lnNyMclg"
}

# 1. 创建容器规格
container_spec_code = 'xsys'
description = 'xsys'

data = [{"level": 2, "bay": 2, "length": "", "width": "", "height": "", "subContainerNum": 4, "checkedList": [],
         "subContainerList": [
             {"subContainerCode": "LA1", "priority": "", "subContainerStatus": "ENABLE", "subContainerLength": "",
              "subContainerWidth": "", "subContainerHeight": "", "subContainerVolume": "",
              "subContainerLoadBearing": "",
              "level": 1, "bay": 1, "subContainerLevel": 1, "subContainerBay": 1, "children": []},
             {"subContainerCode": "LA2", "priority": "", "subContainerStatus": "ENABLE", "subContainerLength": "",
              "subContainerWidth": "", "subContainerHeight": "", "subContainerVolume": "",
              "subContainerLoadBearing": "",
              "level": 1, "bay": 1, "subContainerLevel": 1, "subContainerBay": 2, "children": []},
             {"subContainerCode": "LB1", "priority": "", "subContainerStatus": "ENABLE", "subContainerLength": "",
              "subContainerWidth": "", "subContainerHeight": "", "subContainerVolume": "",
              "subContainerLoadBearing": "",
              "level": 1, "bay": 1, "subContainerLevel": 2, "subContainerBay": 1, "children": []},
             {"subContainerCode": "LB2", "priority": "", "subContainerStatus": "ENABLE", "subContainerLength": "",
              "subContainerWidth": "", "subContainerHeight": "", "subContainerVolume": "",
              "subContainerLoadBearing": "",
              "level": 1, "bay": 1, "subContainerLevel": 2, "subContainerBay": 2, "children": []}], "location": "LEFT",
         "label": "左墙", "essContainerType": "", "containerSpecCode": container_spec_code,
         "description": description,
         "containerSpecStatus": "ENABLE", "containerSpecType": "STATION_STATIC_PUTWALL"}]

r = requests.post(url=create_container_spec_url, data=json.dumps(data), headers=headers)

# 确保容器规格创建成功
assert r.json()['code'] in [0, '0', 'B02008']
print(f'创建容器规格成功: {container_spec_code}')


# 2. 创建1000个工作站
succeed_station_code_lst = []
failed_station_code_lst = []
for i in range(999, 1051):
    station_code = f'xsys{i}'

    params = {
        'stationCode': station_code,
        'stationOperationType': 'OUTBOUND',
        'stationPhysicalType': 'ROBOT',
        'useEssData': 'false'
    }
    # 创建工作站
    r = requests.get(url=create_station_url, params=params)
    # if r.text['code'] == "0":
    if int(json.loads(r.text)['code']) == 0:
        succeed_station_code_lst.append(station_code)
    else:
        failed_station_code_lst.append(station_code)

# 确保所有工作站创建成功
print('创建失败的工作站')
print(failed_station_code_lst)
assert failed_station_code_lst == []
print('所有工作站创建成功')

# 3. 创建1000个播种墙
# ksStationTaskRuleId 需要手动更换, 如果不需要，则不用

succeed_put_wall_code_lst = []
failed_put_wall_code_lst = []
for station_code in succeed_station_code_lst:
    code = station_code
    description1 = description
    tag_id_lst = [code[4:] + '-' + str(i) for i in range(4, 0, -1)]
    sub_container_lst = [
        {
            "subContainerCode": "LA1",
            "subContainerLevel": 1,
            "subContainerBay": 1,
            "subContainerStatus": "ENABLE",
            "putWallSlotCode": "LA1",
            "isSinglePutWallSlot": "FALSE",
            "tagId": code[4:] + "-4",
            "priority": 4,
            "location": "LEFT"
        },
        {
            "subContainerCode": "LA2",
            "subContainerLevel": 1,
            "subContainerBay": 2,
            "subContainerStatus": "ENABLE",
            "putWallSlotCode": "LA2",
            "isSinglePutWallSlot": "FALSE",
            "tagId": code[4:] + "-3",
            "priority": 3,
            "location": "LEFT"
        },
        {
            "subContainerCode": "LB1",
            "subContainerLevel": 2,
            "subContainerBay": 1,
            "subContainerStatus": "ENABLE",
            "putWallSlotCode": "LB1",
            "isSinglePutWallSlot": "FALSE",
            "tagId": code[4:] + "-2",
            "priority": 2,
            "location": "LEFT"
        },
        {
            "subContainerCode": "LB2",
            "subContainerLevel": 2,
            "subContainerBay": 2,
            "subContainerStatus": "ENABLE",
            "putWallSlotCode": "LB2",
            "isSinglePutWallSlot": "FALSE",
            "tagId": code[4:] + "-1",
            "priority": 1,
            "location": "LEFT"
        }
    ]
    data = [
        {
            "containerSpecCode": container_spec_code,
            "containerSpecType": "STATION_STATIC_PUTWALL",
            "description": description1,
            "containerSpecStatus": "ENABLE",
            "level": 2,
            "bay": 2,
            "length": 0,
            "width": 0,
            "height": 0,
            "volume": 0,
            "subContainerNum": 4,
            "subContainerList": sub_container_lst,
            "location": "LEFT",
            "essContainerType": "",
            "putWallDisplayStyle": "MERGE",
            "kind": "STATION_STATIC_PUTWALL",
            "ksStationTaskRuleId": "",
            "code": code,
            "stationCode": station_code,
            "putWallStatus": "ENABLE"
        }
    ]
    r = requests.post(url=create_put_wall_url, data=json.dumps(data, ensure_ascii=False), headers=headers)

    if int(r.json()['code']) == 0:
        succeed_put_wall_code_lst.append(station_code)
    else:
        failed_put_wall_code_lst.append(station_code)


print('创建失败的播种墙')
print(failed_put_wall_code_lst)

assert failed_put_wall_code_lst == []
print('所有播种墙创建成功')


# "ksStation_stationStatus":"OFFLINE"
# 4. 离线工作站和在线工作站存入redis
# 5. 分配离线工作站, 利用redis事务
# 6. 工作站超时，自动离线(或者凌晨2:00分配出去的工作站自动离线)


"""

1. 怎样定制清单
1.1 权利下放
















"""
